package com.online.olsrootserver.controller;

import com.online.olsrootserver.entity.Message;
import com.online.olsrootserver.service.MessageService;
import com.online.olsrootserver.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by littleheng on 2018/4/27
 */
@RestController
public class MessageController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private MessageService messageService;

    /**
     * 分页获取顾客聊天历史消息
     * @param customerId 顾客id
     * @param companyId  企业id
     * @param createTime 第一条消息的创建时间，即分页中最小的消息时间，第一页传""
     * @param pageSize   每页显示消息数
     * @return
     */
    @RequestMapping(value = "/getMessageList", method = RequestMethod.POST)
    public List<Message> getMessageList(@RequestParam("customerId")String customerId,
                                        @RequestParam("companyId")String companyId,
                                        @RequestParam("createTime")String createTime,
                                        @RequestParam("pageSize")int pageSize) {

        List<Message> messages = new ArrayList<>();
        List<String> sessionIds = this.sessionService.getSessionIdList(companyId, customerId);
        for (String sessionId : sessionIds) {
            for (Message message : this.messageService.getMessageList(companyId, sessionId)) {
                messages.add(message);
            }
        }

        if (messages.size() > 0 ) {
            for (int i = 0; i < messages.size(); i++) {
                for (int j = i + 1; j < messages.size(); j++) {
                    if (messages.get(i).getCreateTime().getTime() > messages.get(j).getCreateTime().getTime()) {
                        Message tmpMessage = messages.get(i);
                        messages.set(i, messages.get(j));
                        messages.set(j, tmpMessage);
                    }
                }
            }
        }

        if (messages.size() > 0) {
            List<Message> resultMessage = new ArrayList<>();
            Timestamp lastCreateTime = messages.get(messages.size() - 1).getCreateTime();
            Timestamp signCreateTime = createTime == "" ? lastCreateTime : Timestamp.valueOf(createTime);

            int tmp = 1;
            for (int i = messages.size() - 1; i >=0; i--) {
                if (tmp > pageSize) break;
                if (messages.get(i).getCreateTime().getTime() < signCreateTime.getTime()) {
                    resultMessage.add(messages.get(i));
                    tmp ++;
                }
            }
            return resultMessage;
        } else {
            return null;
        }
    }

    @RequestMapping(value = "/addMessage", method = RequestMethod.GET)
    public int addMessage(@RequestParam("companyId")String companyId) {
        Message message = new Message();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        message.setCompanyId(companyId);
        message.setContent("哈哈哈");
        message.setCreateTime(now);
        message.setDirection(0);
        message.setReadStatus(0);
        message.setType(0);
        message.setUuid("12344555");
        message.setSessionId("1");
        int result =  this.messageService.addMessage(message);
        if (result > 0) {
            System.out.println("添加消息成功");
        } else {
            System.out.println("添加消息失败");
        }
        return result;
    }

    @GetMapping("/sayHello")
    public String sayHello() {
        return "欢迎来到history-manage-server";
    }
}